import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/autopicking_provider.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//自动上架
class AutoPicking extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AutoPicking();
  }
}

//ListWidget
class _AutoPicking extends State<AutoPicking> {
  List<dynamic> _data = List();
  

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      Provider.of<AutoPickingProvider>(context).getList(sendObj: {
        "pageno": 1,
        "pageSize": -1,
        "orderno": ""
      });
    });
  }


  @override
  Widget build(BuildContext context) {
    _data = Provider.of<AutoPickingProvider>(context).listData;
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "自动拣选",
          style: TextStyle(fontSize: 18),
        ),
        actions: <Widget>[
          new IconButton(
            // action button
            padding: EdgeInsets.only(),
            iconSize: 22,
            icon: new Icon(IconData(0xe617, fontFamily: "wuIconFont")),
            onPressed: () {
              Navigator.pushNamed(context, "/search");
            },
          )
        ],
        elevation: 0,
        centerTitle: true,
      ),
      body: LoadBuilder(
        visibility: Provider.of<AutoPickingProvider>(context).netStatus1,
        builder: RefreshIndicator(child:  ListView.separated(
            itemBuilder: (BuildContext context, int index) {
              return TextCartItem(
                data: _data,
                index: index,
                onTapFuns: (String id) {
                  Provider.of<AutoPickingProvider>(context)
                      .getDetailListData(orderid: _data[index]['id']);
                  Navigator.of(context).pushNamed("/autopicking_detail_list",arguments:id);
                },
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return Divider(color: Colors.black26);
            },
            itemCount: _data.length
        ),onRefresh: _refresh)

    ),
      );
  }

  Future <void> _refresh()  async{
    await Future.delayed(Duration(seconds: 1),(){
      return null;
    });
    Provider.of<AutoPickingProvider>(context).getList(sendObj: {
    "pageno": 1,
    "pageSize": -1,
    "orderno": ""
    });
  }
}

class _Text extends Text {
  TextStyle style;
  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<dynamic> data;
  final int index;
  final Function(String id) onTapFuns;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color textColor =
    data[index]['ispickup'] == true ? Color(0xFF2FC480) : Color(0xFF00A5F1);
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.bottomLeft,
                child: _Text("项目名称:${data[index]['projectname']??""}"),
              ),
              Container(
                padding: EdgeInsets.only(top: 10, bottom: 10),
                child: Row(
                  children: <Widget>[
                  RichText(
                    text: TextSpan(
                      text: '拣选状态:',
                      style:
                      TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                      children: <TextSpan>[
                        TextSpan(
                          text: data[index]['orderstatetypename'],
                          style: TextStyle(fontSize: 14.0, color: textColor),
                        ),
                      ],
                    ),
                  ),
                    Container(
                        margin: EdgeInsets.only(left: 20),
                        child: _Text("送达时间:${data[index]['creationTime']}")),
                  ],
                ),
              ),
              // Align(
              //   alignment: Alignment.bottomLeft,
              //   child: _Text("原因:${data[index]['qdperson']}"),
              // )
            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          onTapFuns(data[index]['id']);
        }
      },
    );
  }
}
